
/**   数据库链接  */
var mysql = require('mysql');
var request = require("request");
//查
var lhsxStr = [];
var connection = mysql.createConnection({
  host: '127.0.0.1',
  user: 'root',
  password: 'password',
  port: '3306',
  database: 'table',
  multipleStatements:true,
});
connection.connect();
// 排序

function func(a, b) {
  return a - b;
}
function getsx(arr) {
  let sqlsx = "SELECT * FROM tb_lhsx"
  connection.query(sqlsx, (err, result) => {
    if (err) {
      throw new Error("查询六合生肖失败:"  + err);
    } else {
      for (let i = 0; i < result.length; i++) {
        arr[i] = result[i].sx
      }
    }
  })
}

var times = setInterval(function () {
  // 江苏快三
  addData("https://www.1688-api.com/token/b98876ac694c11edb397337db1ea3104/code/jsks/rows/10.json", 'tb_jsks', '江苏快三');
  // 香港六合
  addData("https://www.1688-api.com/token/e487352695c11edaee5913b26edbc6c/code/hk6/rows/20.json", 'tb_hk', '香港六合');
  // 澳门六合
  addData("https://www.1688-api.com/token/b98876ac694c11edb397337db1ea3104/code/amlhc/rows/10.json", 'tb_am', '澳门六合');
  //  北京pk10
  addData("https://www.1688-api.com/token/b98876ac694c11edb397337db1ea3104/code/bjpk10/rows/10.json", 'tb_bjpk', '北京pk10');
  // 加拿大28
  addData("https://1688-api.com/token/55cbce20671c11ed8514bbf6f2f78802/code/jnd28/rows/20.json", 'tb_jnd', '加拿大28');
  getsx(lhsxStr);
}, 10000);
/** 添加进数据库 */
function addData(url, table, str) {
  let params = {
    url: url,
    method: "get",
    json: true,
    headers: {
      "content-type": "application/json",
    },
  };
  request(params, function (err, res, body) {
    if (body.rows != 0) {
      let sql = ''
      for (var i = 0; i < body.data.length; i++) {
        let strCode =  body.data[body.data.length - i - 1].opencode.replace("+", ",");
        sql +=
          "INSERT IGNORE INTO " + table + " (expect,opencode,opentime) values ('" +
          body.data[body.data.length - i - 1].expect +
          "','" +
          strCode +
          "',  '" +
          body.data[body.data.length - i - 1].opentime +
          "'       );";
      }
      connection.query(sql,(err, result) => {
        if (err) {
          throw new Error("添加失败:" + str + err);
        } else {
          if (result[19].warningCount === 0) {
            console.log(str + "数据添加成功:-----")
            console.log(result)
            ProcessingData(str,table)
          }else{
            console.log(str + "数据添加失败:-----未开奖或服务器错误!")
          }
          
        }
      })
    } else if(body === undefined){
      let time = new Date()
      console.log(str + "数据获取失败:-----" + body.info + "----" + time.toLocaleString())
    }
  })
}
// 处理数据
function ProcessingData(type, table) {
  let sql = "SELECT * FROM " + table + " ORDER BY expect desc limit 20"
  if (type == '香港六合') {
    connection.query(sql, (err, result) => {
      if (err) {
        throw new Error("查询失败:" + type + err);
      } else {
        // 获取到数据
        let res = result
        let ycsql = "SELECT * FROM tb_hk_forecast WHERE iswin='等开'  ORDER BY id desc limit 20"
        connection.query(ycsql, (err, result) => {
          if (err) {
            throw new Error("查询失败:" + type + err);
          } else {
            
            let ycnum = []
            if (result.length == 0) {
              // 没有预测 需要重新添加
              // 添加预测
              // 获取预测值
              ycnum = Forecast('tb_hk_forecast', type, res)
              addHkYcType(1, res, ycnum)
              addHkYcType(2, res, ycnum)
              addHkYcType(3, res, ycnum)
              addHkYcType(4, res, ycnum)
            } else {
              // 有预测
              // 对比预测数据
              let strarr = res[0].opencode.split(',')
              for (let i = 0; i < result.length; i++) {
                if (parseInt(res[0].expect) >= parseInt(result[i].start_issue) && parseInt(res[0].expect) <= parseInt(result[i].end_issue)) {
                  // 属于计划期数内
                  // 判断是否正确
                  // 平特1肖
                  // 判断是否正确
                  if (result[i].forecast_type == 2) {
                    // 特码大小
                    // 判断是否正确
                    if (getlhDX(strarr[6]) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }
                  }
                  if (result[i].forecast_type == 3) {
                    // 特码单双
                    // 判断是否正确
                    if (getDS(strarr[6]) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }
                  }
                  if (result[i].forecast_type == 4) {
                    // 特码24码
                    // 判断是否正确
                    for (let k = 0; k < strarr.length; k++) {
                      let ycstr = result[i].forecast_content.split(",")
                      for (let k = 0; k < ycstr.length; k++) {
                        if (parseInt(strarr[6]) == parseInt(ycstr[k])) {
                          // 正确
                          isyes = 1
                          break;
                        } else {
                          // 错误
                          isyes = 0
                        }
                      }
                    }
                  }

                  if (result[i].forecast_type == 1) {
                    for (let k = 0; k < strarr.length; k++) {
                      if (getlhSX(strarr[k]) == result[i].forecast_content) {
                        // 正确
                        isyes = 1
                        break;
                      } else {
                        // 错误
                        isyes = 0
                      }
                    }
                  }
                  if (isyes === 1) {
                    // 正确的 
                    // 需要重新添加计划
                    editData('tb_hk_forecast', {
                      'id': result[i].id,
                      'iswin': '中'
                    }, type)
                    ycnum = Forecast('tb_hk_forecast', type, res)
                    addHkYcType(result[i].forecast_type, res, ycnum)
                  } else if (isyes === 0 && parseInt(res[0].expect) == parseInt(result[i].end_issue)) {
                    // 最后一期错误
                    // 判断本次计划错误
                    // 添加新的计划
                    editData('tb_hk_forecast', {
                      'id': result[i].id,
                      'iswin': '错'
                    }, type)
                    // 添加计划
                    ycnum = Forecast('tb_hk_forecast', type, res)
                    addHkYcType(result[i].forecast_type, res, ycnum)
                  }
                } else {
                  // 计划期数外
                  // 判断为错误
                  editData('tb_hk_forecast', {
                    'id': result[i].id,
                    'iswin': '错'
                  }, type)
                  // 添加计划
                  ycnum = Forecast('tb_hk_forecast', type, res)
                  addHkYcType(result[i].forecast_type, res, ycnum)
                }
              }
            }
          }
        })
      }
    })
  } else if (type == '澳门六合') {
    connection.query(sql, (err, result) => {
      if (err) {
        throw new Error("查询失败:" + type + err);
      } else {
        // 获取到数据
        let res = result // 开奖数据
        let ycsql = "SELECT * FROM tb_am_forecast  WHERE iswin='等开' ORDER BY id desc limit 20"
        connection.query(ycsql, (err, result) => {
          if (err) {
            throw new Error("查询失败:" + type + err);
          } else {
            let ycnum = []
            ycnum = Forecast('tb_am_forecast', type, res)
            if (result.length == 0) {
              // 没有预测 需要重新添加
              // 添加预测
              // 获取预测值
              addAmYcType(1, res, ycnum)
              addAmYcType(2, res, ycnum)
              addAmYcType(3, res, ycnum)
              addAmYcType(4, res, ycnum)
            } else {
              // 有预测
              // 对比预测数据
              let strarr = res[0].opencode.split(',')
              let isyes = 0;
              for (let i = 0; i < result.length; i++) {
                if (parseInt(res[0].expect) >= parseInt(result[i].start_issue) && parseInt(res[0].expect) <= parseInt(result[i].end_issue)) {
                  // 属于计划期数内
                  // 判断是否正确
                  // 平特1肖
                  // 判断是否正确
                  if (result[i].forecast_type == 2) {
                    // 特码大小
                    // 判断是否正确
                    if (getlhDX(strarr[6]) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }
                  }
                  if (result[i].forecast_type == 3) {
                    // 特码单双
                    // 判断是否正确
                    if (getDS(strarr[6]) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }
                  }
                  if (result[i].forecast_type == 4) {
                    // 特码24码
                    // 判断是否正确
                    for (let k = 0; k < strarr.length; k++) {
                      let ycstr = result[i].forecast_content.split(",")
                      for (let k = 0; k < ycstr.length; k++) {
                        if (parseInt(strarr[6]) == parseInt(ycstr[k])) {
                          // 正确
                          isyes = 1
                          break;
                        } else {
                          // 错误
                          isyes = 0
                        }
                      }
                    }
                  }

                  if (result[i].forecast_type == 1) {
                    for (let k = 0; k < strarr.length; k++) {
                      if (getlhSX(strarr[k]) == result[i].forecast_content) {
                        // 正确
                        isyes = 1
                        break;
                      } else {
                        // 错误
                        isyes = 0
                      }
                    }
                  }
                  if (isyes === 1) {
                    // 正确的 
                    // 需要重新添加计划
                    editData('tb_am_forecast', {
                      'id': result[i].id,
                      'iswin': '中'
                    }, type)
                    ycnum = Forecast('tb_hk_forecast', type, res)
                    addAmYcType(result[i].forecast_type, res, ycnum)
                  } else if (isyes === 0 && parseInt(res[0].expect) == parseInt(result[i].end_issue)) {
                    // 最后一期错误
                    // 判断本次计划错误
                    // 添加新的计划
                    editData('tb_am_forecast', {
                      'id': result[i].id,
                      'iswin': '错'
                    }, type)
                    // 添加计划
                    ycnum = Forecast('tb_hk_forecast', type, res)
                    addAmYcType(result[i].forecast_type, res, ycnum)
                  }
                } else {
                  // 计划期数外
                  // 判断为错误
                  editData('tb_am_forecast', {
                    'id': result[i].id,
                    'iswin': '错'
                  }, type)
                  // 添加计划
                  ycnum = Forecast('tb_hk_forecast', type, res)
                  addAmYcType(result[i].forecast_type, res, ycnum)
                }
              }
            }
          }
        })
      }
    })
  } else if (type == '北京pk10') {
    connection.query(sql, (err, result) => {
      if (err) {
        throw new Error("查询失败:" + type + err);
      } else {
        // 获取到数据
        let res = result
        let ycsql = "SELECT * FROM tb_bjpk_forecast WHERE iswin='等开'  ORDER BY id desc limit 20"
        connection.query(ycsql, (err, result) => {
          if (err) {
            throw new Error("查询失败:" + type + err);
          } else {
            let ycnum = []
            if (result.length == 0) {
              // 没有预测 需要重新添加
              // 添加预测
              // 获取预测值
              ycnum = Forecast('tb_bjpk_forecast', type, res)
              addBjpkYcType(1, res, ycnum)
              addBjpkYcType(2, res, ycnum)
            } else {
              // 有预测
              // 对比预测数据
              let strarr = res[0].opencode.split(',')
              for (let i = 0; i < result.length; i++) {
                if (parseInt(res[0].expect) >= parseInt(result[i].start_issue) && parseInt(res[0].expect) <= parseInt(result[i].end_issue)) {
                  let isyes = 0;
                  let ycstr = result[i].forecast_content.split(",")
                  // 属于计划期数内
                  // 判断是否正确
                  if (result[i].forecast_type == 1) {
                    // 冠军5码
                    for (let k = 0; k < ycstr.length; k++) {
                      if (parseInt(strarr[0]) == parseInt(ycstr[k])) {
                        // 正确
                        isyes = 1
                        break;
                      } else {
                        // 错误
                        isyes = 0
                      }
                    }
                  } else if (result[i].forecast_type == 2) {
                    // 亚军5码
                    for (let k = 0; k < ycstr.length; k++) {
                      if (parseInt(strarr[0]) == parseInt(ycstr[k])) {
                        // 正确
                        isyes = 1
                        break;
                      } else {
                        // 错误
                        isyes = 0
                      }
                    }
                  }
                  if (isyes === 1) {
                    // 正确的 
                    // 需要重新添加计划
                    editData('tb_bjpk_forecast', {
                      'id': result[i].id,
                      'iswin': '中'
                    }, type)
                    ycnum = Forecast('tb_bjpk_forecast', type, res)
                    addBjpkYcType(result[i].forecast_type, res, ycnum)
                  } else if (isyes === 0 && parseInt(res[0].expect) == parseInt(result[i].end_issue)) {
                    // 最后一期错误
                    // 判断本次计划错误
                    // 添加新的计划
                    editData('tb_bjpk_forecast', {
                      'id': result[i].id,
                      'iswin': '错'
                    }, type)
                    // 添加计划
                    ycnum = Forecast('tb_bjpk_forecast', type, res)
                    addBjpkYcType(result[i].forecast_type, res, ycnum)
                  }
                } else {
                  // 计划期数外
                  // 判断为错误
                  editData('tb_bjpk_forecast', {
                    'id': result[i].id,
                    'iswin': '错'
                  }, type)
                  // 添加计划
                  ycnum = Forecast('tb_bjpk_forecast', type, res)
                  addBjpkYcType(result[i].forecast_type, res, ycnum)
                }
              }
            }
          }
        })
      }
    })
  } else if (type == '江苏快三') {
    connection.query(sql, (err, result) => {
      if (err) {
        throw new Error("查询失败:" + type + err);
      } else {
        // 获取到数据
        let res = result
        let ycsql = "SELECT * FROM tb_jsks_forecast WHERE iswin='等开'  ORDER BY id desc limit 20"
        connection.query(ycsql, (err, result) => {
          if (err) {
            throw new Error("查询失败:" + type + err);
          } else {
            let ycnum = []
            if (result.length == 0) {
              // 没有预测 需要重新添加
              // 添加预测
              // 获取预测值
              ycnum = Forecast('tb_jsks_forecast', type, res)
              addJsksYcType(1, res, ycnum)
              addJsksYcType(2, res, ycnum)
              addJsksYcType(3, res, ycnum)
            } else {
              // 有预测
              // 对比预测数据
              for (let i = 0; i < result.length; i++) {
                if (parseInt(res[0].expect) >= parseInt(result[i].start_issue) && parseInt(res[0].expect) <= parseInt(result[i].end_issue)) {
                  let isyes = 0;
                  let ycstr = result[i].forecast_content.split(",")
                  // 属于计划期数内
                  // 判断是否正确
                  if (result[i].forecast_type == 1) {
                    // 大小
                    if (getKsDX(getAndValues(res[0].opencode)) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }

                  } else if (result[i].forecast_type == 2) {
                    // 单双
                    if (getDS(getAndValues(res[0].opencode)) == result[i].forecast_content) {
                      // 正确
                      isyes = 1

                    } else {
                      // 错误
                      isyes = 0
                    }
                  } else {
                    for (let k = 0; k < ycstr.length; k++) {
                      if (getAndValues(res[0].opencode) == parseInt(ycstr[k])) {
                        // 正确
                        isyes = 1
                        break;
                      } else {
                        // 错误
                        isyes = 0
                      }
                    }
                  }
                  if (isyes === 1) {
                    // 正确的 
                    // 需要重新添加计划
                    editData('tb_jsks_forecast', {
                      'id': result[i].id,
                      'iswin': '中'
                    }, type)
                    ycnum = Forecast('tb_jsks_forecast', type, res)
                    addJsksYcType(result[i].forecast_type, res, ycnum)
                  } else if (isyes === 0 && parseInt(res[0].expect) == parseInt(result[i].end_issue)) {
                    // 最后一期错误
                    // 判断本次计划错误
                    // 添加新的计划
                    editData('tb_jsks_forecast', {
                      'id': result[i].id,
                      'iswin': '错'
                    }, type)
                    // 添加计划
                    ycnum = Forecast('tb_jsks_forecast', type, res)
                    addJsksYcType(result[i].forecast_type, res, ycnum)
                  }
                } else {
                  // 计划期数外
                  // 判断为错误
                  // 添加新的计划
                  editData('tb_jsks_forecast', {
                    'id': result[i].id,
                    'iswin': '错'
                  }, type)
                  // 添加计划
                  ycnum = Forecast('tb_jsks_forecast', type, res)
                  addJsksYcType(result[i].forecast_type, res, ycnum)
                }
              }
            }
          }
        })
      }
    })
  } else if (type == '加拿大28') {
    connection.query(sql, (err, result) => {
      if (err) {
        throw new Error("查询失败:" + type + err);
      } else {
        // 获取到数据
        let res = result
        let ycsql = "SELECT * FROM tb_jnd_forecast WHERE iswin='等开'  ORDER BY id desc limit 20"
        connection.query(ycsql, (err, result) => {
          if (err) {
            throw new Error("查询失败:" + type + err);
          } else {
            let ycnum = []
            if (result.length == 0) {
              // 没有预测 需要重新添加
              // 添加预测
              // 获取预测值
              console.log('错')
              ycnum = Forecast('tb_jnd_forecast', type, res)
              addJndYcType(1, res, ycnum)
              addJndYcType(2, res, ycnum)
              addJndYcType(3, res, ycnum)
            } else {
              // 有预测
              // 对比预测数据
              for (let i = 0; i < result.length; i++) {
                if (parseInt(res[0].expect) >= parseInt(result[i].start_issue) && parseInt(res[0].expect) <= parseInt(result[i].end_issue)) {
                  let isyes = 0;
                  let ycstr = result[i].forecast_content.split(",")
                  // 属于计划期数内
                  // 判断是否正确
                  if (result[i].forecast_type == 1) {
                    // 大小
                    if (get28DX(getAndValues(res[0].opencode)) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }

                  } else if (result[i].forecast_type == 2) {
                    // 单双
                    if (getDS(getAndValues(res[0].opencode)) == result[i].forecast_content) {
                      // 正确
                      isyes = 1
                    } else {
                      // 错误
                      isyes = 0
                    }
                  } else {
                    for (let k = 0; k < ycstr.length; k++) {
                      if (getAndValues(res[0].opencode) == parseInt(ycstr[k])) {
                        // 正确
                        isyes = 1
                        break;
                      } else {
                        // 错误
                        isyes = 0
                      }
                    }
                  }
                  if (isyes === 1) {
                    // 正确的 
                    // 需要重新添加计划
                    editData('tb_jnd_forecast', {
                      'id': result[i].id,
                      'iswin': '中'
                    }, type)
                    ycnum = Forecast('tb_jnd_forecast', type, res)
                    addJndYcType(result[i].forecast_type, res, ycnum)
                  } else if (isyes === 0 && parseInt(res[0].expect) == parseInt(result[i].end_issue)) {
                    // 最后一期错误
                    // 判断本次计划错误
                    // 添加新的计划
                    editData('tb_jnd_forecast', {
                      'id': result[i].id,
                      'iswin': '错'
                    }, type)
                    // 添加计划
                    ycnum = Forecast('tb_jnd_forecast', type, res)
                    addJndYcType(result[i].forecast_type, res, ycnum)
                  }
                } else {
                  // 计划期数外
                  // 判断为错误
                  // 添加新的计划
                  editData('tb_jnd_forecast', {
                    'id': result[i].id,
                    'iswin': '错'
                  }, type)
                  // 添加计划
                  ycnum = Forecast('tb_jnd_forecast', type, res)
                  addJndYcType(result[i].forecast_type, res, ycnum)
                }
              }
            }
          }
        })
      }
    })
  }

}
// 添加计划
function AddForecast(table, data, type) {
  let sql = "INSERT IGNORE INTO " + table + " (forecast_type,start_issue,end_issue,forecast_content,iswin) values( '" +
    data.forecast_type +
    "','" +
    data.start_issue +
    "','" +
    data.end_issue +
    "','" +
    data.forecast_content +
    "','" +
    data.iswin +
    "'       )";
  connection.query(sql, (err, result) => {
    if (err) {
      throw new Error("添加计划失败:" + type + err);
    } else {
      if (result.affectedRows === 0) {
        console.log(type + "计划添加失败:-----")
      } else {
        console.log(type + "计划添加成功:-----")
      }
    }
  })
}
// 计划算法处理
function Forecast(table, type, kjdata) {
  if (type == '香港六合' || type == '澳门六合') {
    // 六合计划24码处理
    // 
    let longnum = []; // 20期开奖数据
    let numwhole = []; // 预测的24码
    for (let i = 0; i < kjdata.length; i++) {
      let str = kjdata[i].opencode.split(',')
      for (let y = 0; y < str.length; y++) {
        longnum[i * str.length + y] = str[y]
      }
    }
    for (let k = 0; k < longnum.length; k++) {
      let str1 = kjdata[parseInt(parseInt(longnum[k]) / 7)].opencode.split(',')
      if ((parseInt(longnum[k]) % 7) == 0) {
        numwhole[k] = str1[0]
      } else {
        numwhole[k] = str1[parseInt(parseInt(longnum[k]) % 7) - 1]
      }
    }
    let num24 = Array.from(new Set(numwhole))
    if (num24.length > 25) {
      num24.length = 24
    }
    return num24;
  } else if (type == '北京pk10') {
    // 赛车pk10计划处理
    let longnu = []; // 最新期开奖数据处理
    let numwhole = [] //预测的5码
    let str = kjdata[0].opencode.split(',')
    for (let y = 0; y < str.length; y++) {
      longnu[y] = str[y]
    }
    for (let k = 0; k < 5; k++) {
      if ((parseInt(longnu[k]) % 10) == 0) {
        numwhole[k] = longnu[9]
      } else {
        numwhole[k] = longnu[parseInt(parseInt(longnu[k]) % 10) - 1]
      }
    }
    return numwhole;
  } else if (type == '江苏快三' || type == '加拿大28') {
    let arr
    if (type == '江苏快三') {
      arr = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
    } else {
      arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
    }
    let randomNumber = function () {
      return 0.5 - Math.random()
    }
    arr.sort(randomNumber)
    arr.length = parseInt(arr.length / 2)
    arr.sort(function (a, b) {
      return a - b
    })
    return arr
  }
}

// 香港六合计划
function addHkYcType(type, kjdata, ycnum) {
  // type1=>平特1肖 2=>特码大小 3=>特码单双 4=> 特码24码
  // 获取下一期预测
  let nextIssue = parseInt(kjdata[0].expect) + 1
  // 获取目标期预测
  let targetIssue = parseInt(kjdata[0].expect) + 3
  let forecastContent = '';
  if (type == 1) {
    forecastContent = getlhSX(ycnum[23])
  } else if (type == 2) {
    let Xnum = 0 // 小
    let Danum = 0 //大
    for (let i = 0; i < ycnum.length; i++) {
      if (getlhDX(ycnum[i]) === '大') {
        Danum++;
      } else {
        Xnum++;
      }
    }
    // 获取预测值的大小
    if (Xnum > Danum) {
      forecastContent = '小';
    } else {
      forecastContent = '大';
    }
  } else if (type == 3) {
    let Snum = 0 // 双
    let Dannum = 0 //单
    for (let i = 0; i < ycnum.length; i++) {
      if (getDS(ycnum[i]) === '单') {
        Dannum++;
      } else {
        Snum++;
      }
    }
    // 获取预测值的单双
    if (Snum > Dannum) {
      forecastContent = '双';
    } else {
      forecastContent = '单';
    }
  } else {
    forecastContent = ycnum.sort(func).toString();
  }
  const data = {
    'forecast_type': type,
    'start_issue': nextIssue,
    'end_issue': targetIssue,
    'forecast_content': forecastContent,
    'iswin': '等开'
  };
  AddForecast('tb_hk_forecast', data, '香港六合计划')
}

// 澳门六合计划
function addAmYcType(type, kjdata, ycnum) {
  // type1=>平特1肖 2=>特码大小 3=>特码单双 4=> 特码24码
  // 获取下一期预测
  let nextIssue = parseInt(kjdata[0].expect) + 1
  // 获取目标期预测
  let targetIssue = parseInt(kjdata[0].expect) + 3
  let forecastContent = '';
  if (type == 1) {
    forecastContent = getlhSX(ycnum[23])
  } else if (type == 2) {
    let Xnum = 0 // 小
    let Danum = 0 //大
    for (let i = 0; i < ycnum.length; i++) {
      if (getlhDX(ycnum[i]) === '大') {
        Danum++;
      } else {
        Xnum++;
      }
    }
    // 获取预测值的大小
    if (Xnum > Danum) {
      forecastContent = '小';
    } else {
      forecastContent = '大';
    }
  } else if (type == 3) {
    let Snum = 0 // 双
    let Dannum = 0 //单
    for (let i = 0; i < ycnum.length; i++) {
      if (getDS(ycnum[i]) === '单') {
        Dannum++;
      } else {
        Snum++;
      }
    }
    // 获取预测值的单双
    if (Snum > Dannum) {
      forecastContent = '双';
    } else {
      forecastContent = '单';
    }
  } else {
    forecastContent = ycnum.sort(func).toString();
  }
  const data = {
    'forecast_type': type,
    'start_issue': nextIssue,
    'end_issue': targetIssue,
    'forecast_content': forecastContent,
    'iswin': '等开'
  };
  AddForecast('tb_am_forecast', data, '澳门六合计划')
}
// pk10计划
function addBjpkYcType(type, kjdata, ycnum) {
  // 获取下一期预测
  let nextIssue = parseInt(kjdata[0].expect) + 1
  // 获取目标期预测
  let targetIssue = parseInt(kjdata[0].expect) + 3
  let forecastContent = '';
  if (type == 1) {
    forecastContent = ycnum.sort(func).toString();
  } else {
    forecastContent = ycnum.sort(func).toString();
  }
  const data = {
    'forecast_type': type,
    'start_issue': nextIssue,
    'end_issue': targetIssue,
    'forecast_content': forecastContent,
    'iswin': '等开'
  };
  AddForecast('tb_bjpk_forecast', data, '北京pk10计划')
}

// 快三计划

function addJsksYcType(type, kjdata, ycnum) {
  // type1=>和值大小 2=>和值单双 3=>和值7码

  // 获取下一期预测
  let nextIssue = parseInt(kjdata[0].expect) + 1
  // 获取目标期预测
  let targetIssue = parseInt(kjdata[0].expect) + 3

  let forecastContent = '';
  if (type == 1) {
    let Xnum = 0 // 小
    let Danum = 0 //大
    for (let i = 0; i < ycnum.length; i++) {
      if (getKsDX(ycnum[i]) === '大') {
        Danum++;
      } else {
        Xnum++;
      }
    }
    // 获取预测值的大小
    if (Xnum > Danum) {
      forecastContent = '小';
    } else {
      forecastContent = '大';
    }
  } else if (type == 2) {
    let Snum = 0 // 双
    let Dannum = 0 //单
    for (let i = 0; i < ycnum.length; i++) {
      if (getDS(ycnum[i]) === '单') {
        Dannum++;
      } else {
        Snum++;
      }
    }
    // 获取预测值的单双
    if (Snum > Dannum) {
      forecastContent = '双';
    } else {
      forecastContent = '单';
    }
  } else {
    forecastContent = ycnum.sort(func).toString();
  }
  const data = {
    'forecast_type': type,
    'start_issue': nextIssue,
    'end_issue': targetIssue,
    'forecast_content': forecastContent,
    'iswin': '等开'
  };
  AddForecast('tb_jsks_forecast', data, '江苏快三计划')
}

function addJndYcType(type, kjdata, ycnum) {
  // type1=>和值大小 2=>和值单双 3=>和值7码

  // 获取下一期预测
  let nextIssue = parseInt(kjdata[0].expect) + 1
  // 获取目标期预测
  let targetIssue = parseInt(kjdata[0].expect) + 3

  let forecastContent = '';
  if (type == 1) {
    let Xnum = 0 // 小
    let Danum = 0 //大
    for (let i = 0; i < ycnum.length; i++) {
      if (get28DX(ycnum[i]) === '大') {
        Danum++;
      } else {
        Xnum++;
      }
    }
    // 获取预测值的大小
    if (Xnum > Danum) {
      forecastContent = '小';
    } else {
      forecastContent = '大';
    }
  } else if (type == 2) {
    let Snum = 0 // 双
    let Dannum = 0 //单
    for (let i = 0; i < ycnum.length; i++) {
      if (getDS(ycnum[i]) === '单') {
        Dannum++;
      } else {
        Snum++;
      }
    }
    // 获取预测值的单双
    if (Snum > Dannum) {
      forecastContent = '双';
    } else {
      forecastContent = '单';
    }
  } else {
    forecastContent = ycnum.sort(func).toString();
  }
  const data = {
    'forecast_type': type,
    'start_issue': nextIssue,
    'end_issue': targetIssue,
    'forecast_content': forecastContent,
    'iswin': '等开'
  };
  AddForecast('tb_jnd_forecast', data, '加拿大28计划')
}

// 更新中奖数据
function editData(table, data, type) {
  let sql = "update " + table + " set iswin='" + data.iswin + "' where id=" + data.id
  connection.query(sql, (err, result) => {
    if (err) {
      throw new Error("修改计划失败:" + type + err);
    } else {
      if (result.affectedRows === 0) {
        console.log(type + "计划修改失败:-----")
      } else {
        console.log(type + "计划修改成功:-----")
      }
    }
  })
}

// 六合大小
function getlhDX(num) {
  if (parseInt(num) >= 1 && parseInt(num) <= 24) {
    return '小'
  } else {
    return '大'
  }
}

// 返回单双
function getDS(num) {
  if (parseInt(num) % 2 == 1) {
    return '单'
  } else {
    return '双'
  }
}

// 快三大小
function getKsDX(num) {
  if (parseInt(num) >= 3 && parseInt(num) <= 10) {
    return '小'
  } else {
    return '大'
  }
}

// 28大小
function get28DX(num) {
  if (parseInt(num) >= 0 && parseInt(num) <= 14) {
    return '小'
  } else {
    return '大'
  }
}

// 计算3数和值
// 传入opencode字符串
function getAndValues(str) {
  let arr = str.split(',')
  let num = 0
  for (let i = 0; i < arr.length; i++) {
    num += parseInt(arr[i])
  }
  return num;
}
// 获取六合生肖
function getlhSX(num) {
  // 获取到数据
  if (parseInt(num) % 12 == 0) {
    return lhsxStr[11];
  } else {
    return lhsxStr[parseInt(num) % 12 - 1];
  }
}
